package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class setmealController {
    @Reference
    private SetmealService setmealService;
    @Reference
    private OrderService orderService;
    //套餐预约占比饼形图
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        Map<String,Object> map = new HashMap<>();
        List<String> months = new ArrayList();
        Calendar calendar = Calendar.getInstance();//获得日历对象，模拟时间就是当前时间
        //计算过去10天
        calendar.add(Calendar.DATE,-10);
        for (int i = 0; i < 10; i++) {
            //获取当前时间往后一个月的时间
            calendar.add(Calendar.DATE,1);
            Date date=calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM.dd").format(date));
        }
        map.put("months",months);
        Map<String,Object> data = new HashMap<>();
        //根据日期查询套餐id
        List<Integer> setmealId= orderService.findSetmealIdByDate(months);
        //根据日期计数套餐预约数量
        System.out.println(setmealId);
        List<Map<String, Object>> setmealCount = setmealService.findSetmealCount(setmealId);
        data.put("setmealCount",setmealCount);
        try {
            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> map1 : setmealCount) {
                String name = (String) map1.get("name");//套餐名称
                setmealNames.add(name);
            }
            data.put("setmealNames",setmealNames);
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
    @RequestMapping("/getDateSetmeal")
    public Result getDateSetmeal(@RequestBody Date[] dateRange) {
        Map<String, Object> data = new HashMap<>();
        String start = new SimpleDateFormat("yyyy.MM.dd").format(dateRange[0]);
        String end = new SimpleDateFormat("yyyy.MM.dd").format(dateRange[1]);
        List<Integer> setmealId = orderService.findSetmealData(start, end);
        //根据日期计数套餐预约数量
        if (setmealId != null) {
            List<Map<String, Object>> setmealCount = setmealService.findSetmealCount(setmealId);
            data.put("setmealCount", setmealCount);
            try {
                List<String> setmealNames = new ArrayList<>();
                for (Map<String, Object> map1 : setmealCount) {
                    String name = (String) map1.get("name");//套餐名称
                    setmealNames.add(name);
                }
                data.put("setmealNames", setmealNames);
                return new Result(true, "", data);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,"");
            }
        }
        return null;
    }

}
